package com.glory.wxelectricitypda.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.wxelectricitypda.R;
import com.glory.wxelectricitypda.adapter.LotListBeanAdapter;
import com.glory.wxelectricitypda.base.BaseDialogActivity;
import com.glory.wxelectricitypda.bean.LotBean;
import com.glory.wxelectricitypda.bean.ResponseBean;
import com.glory.wxelectricitypda.utils.NetUtils;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_AddViewEntity;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

public class WorkBatchEndActivity extends BaseDialogActivity {

    @BindView(R.id.etEqpId)
    FreeUI_ClearEditText etEqpId;
    @BindView(R.id.etLotId)
    FreeUI_ClearEditText etLotId;
    @BindView(R.id.rvLotList)
    RecyclerView rvLotList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private LotListBeanAdapter mLotListBeanAdapter;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_work_batch_begin;
    }

    @Override
    protected void initView() {
        rvLotList.setLayoutManager(new LinearLayoutManager(WorkBatchEndActivity.this));
        rvLotList.setItemAnimator(new DefaultItemAnimator());
        rvLotList.addItemDecoration(new DividerItemDecoration(WorkBatchEndActivity.this, 1));
        mLotListBeanAdapter = new LotListBeanAdapter(null);
        mLotListBeanAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mLotListBeanAdapter.isFirstOnly(true);
        rvLotList.setAdapter(mLotListBeanAdapter);
    }

    @Override
    protected void initData() {

        etLotId.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (start == 0 && before == 0 && count > 1) {
                    JSONObject jsonObject = new JSONObject();
                    JSONObject jsonObject1 = new JSONObject();
                    try {
                        jsonObject1.put("EQPID", etEqpId.getText().toString());
                        jsonObject1.put("ACTIONTYPE", "BYLOT");
                        jsonObject1.put("LOTID", etLotId.getText().toString());
                        jsonObject.put("Data",jsonObject1);
                        jsonObject.put("MessageName","EUIT.EUITTRACKOUT");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    NetUtils.requestPostMesForwardUtils(jsonObject,this, new FreeUI_DialogEntityCallBack<ResponseBean<Object>>(
                            new TypeToken<ResponseBean<Object>>() {
                            }.getType(), getSupportFragmentManager(), this) {
                        @Override
                        public void onSuccess(Response<ResponseBean<Object>> response) {
                            if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                                showToast("出站成功", true);
                                LotBean lotBean = new LotBean(etLotId.getText().toString(), etEqpId.getText().toString());
                                mLotListBeanAdapter.addData(lotBean);
                            }
                        }

                        @Override
                        public void onError(Response<ResponseBean<Object>> response) {
                            super.onError(response);
                            loadError(response.getException(), "EUITTRACKOUT");
                        }
                    });
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    protected FreeUI_AddViewEntity getBaseEntity() {
        freeUI_titleView.setTitle("批量出站");
        return new FreeUI_AddViewEntity("header", llMain);
    }
}

